package com.ms.education;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * <p>Project:education-GetRequestBody
 * <p>Powered by ms on 2023-02-20 04:22:47
 *
 * @author ms
 * @version 1.0
 * @since 1.8
 */
public class GetRequestBody {

    public static String getBodyString(HttpServletRequest request){
        StringBuilder res = new StringBuilder();
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(request.getInputStream()));
            String line;
            while ((line= reader.readLine())!=null){
                res.append(line);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return res.toString();
    }

    public static <T> T bodyToBean(HttpServletRequest request,Class<T> valueType){
        String bodyString = getBodyString(request);
        return JSONUtil.toBean(bodyString, valueType);
    }

}
